package com.atguigu.gulimall.order.web;

import com.atguigu.common.exception.NoStockException;
import com.atguigu.common.to.MemberEntityVo;
import com.atguigu.gulimall.order.entity.OrderEntity;
import com.atguigu.gulimall.order.entity.OrderItemEntity;
import com.atguigu.gulimall.order.interceptor.LoginUserInterceptor;
import com.atguigu.gulimall.order.service.OrderItemService;
import com.atguigu.gulimall.order.service.OrderService;
import com.atguigu.gulimall.order.vo.OrderConfirmVo;
import com.atguigu.gulimall.order.vo.OrderSubmitVo;
import com.atguigu.gulimall.order.vo.SubmitOrderResponseVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import java.util.List;
import java.util.concurrent.ExecutionException;

@Controller
public class OrderWebController {

    @Autowired
    private OrderService orderService;
    @Autowired
    private OrderItemService orderItemService;

    @GetMapping("/confirm.html")
    public String confirm(Model model) throws ExecutionException, InterruptedException {
        OrderConfirmVo orderConfirmVo = orderService.confirmOrder();
        model.addAttribute("orderConfirmData", orderConfirmVo);
        return "confirm";
    }

    @GetMapping("/list.html")
    public String orderList(Model model) {
        // 查询用户所有订单
        MemberEntityVo memberEntityVo = LoginUserInterceptor.threadLocal.get();
        List<OrderEntity> orderList = orderService.list(
                new QueryWrapper<OrderEntity>()
                        .eq("member_id", memberEntityVo.getId())
        );
        // 查询所有订单项
        for (OrderEntity order : orderList) {
            List<OrderItemEntity> orderItemList = orderItemService.list(
                    new QueryWrapper<OrderItemEntity>()
                            .eq("order_sn", order.getOrderSn())
            );
            order.setOrderItemList(orderItemList);
        }
        model.addAttribute("orders", orderList);
        return "list";
    }

    /**
     * 提交下单功能
     *
     * @param orderSubmitVo
     * @return
     */
    @PostMapping("/submitOrder")
    public String submitOrder(OrderSubmitVo orderSubmitVo, Model model, RedirectAttributes redirectAttributes) {
        try {
            SubmitOrderResponseVo responseVo = orderService.submitOrder(orderSubmitVo);
            if (responseVo.getCode() == 0) {
                //跳转到支付页面
                model.addAttribute("submitOrderResp", responseVo);
                return "pay";
            } else {
                String msg = "下单失败;";
                switch (responseVo.getCode()) {
                    case 1:
                        msg += "令牌校验失败";
                        break;
                    case 2:
                        msg += "库存锁定失败;商品库存不足";
                        break;
                }
                //重定向会订单确认页面
                redirectAttributes.addFlashAttribute("msg", msg);
                return "redirect:http://order.gulimall.com/confirm.html";
            }
        } catch (Exception e) {
            if (e instanceof NoStockException) {
                redirectAttributes.addFlashAttribute("msg", e.getMessage());
            } else {
                redirectAttributes.addFlashAttribute("msg", "未知异常");
            }
            return "redirect:http://order.gulimall.com/confirm.html";
        }
    }
}
